package com.tiger.userClient.http.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiger.db.entity.App;
import com.tiger.userClient.Db;
import com.tiger.userClient.caspOpeapi.Openapi;
import com.tiger.userClient.interceptor.RequireAuth;
import com.tiger.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/task")
public class TaskController {
    @Value("${OA_APPID}")
    private String OA_APPID;
    private final Db db;
    private final Openapi openapi;

    public TaskController(Db db, Openapi openapi) {
        this.db = db;
        this.openapi = openapi;
    }


    @GetMapping("test/{userKey}")
    public Object test(@PathVariable String userKey) {
        List<HashMap> tasks = openapi.taskList(userKey, 1);
        return tasks;
    }

    @GetMapping("list")
    @RequireAuth
    public Object list(@RequestAttribute("userKey") String userKey) {



        List<HashMap> tasks = openapi.taskList(userKey, 1);
        List<String> appIds = new ArrayList<>();
        List<HashMap<String, Object>> rows = new ArrayList<>();
        if(tasks != null && !tasks.isEmpty()) {
            for(HashMap item : tasks) {
                if(Objects.equals((String) item.get("isExpireStatus"), "0")) {
                    continue;
                }
                if(item.get("appId") == null) continue;
                String appId = (String) item.get("appId");
                if(!appIds.contains(appId)) {
                    appIds.add(appId);
                }
            }

            if(!appIds.isEmpty()) {
                List<App> apps = db.app.list(new LambdaQueryWrapper<App>().select(App::getName, App::getSourceKey).in(App::getSourceKey, appIds));
                if(!apps.isEmpty()) {
                    for(HashMap item : tasks) {
                        if(Objects.equals((String) item.get("isExpireStatus"), "0")) {
                            continue;
                        }
                        if(item.get("appId") == null) continue;
                        String appId = (String) item.get("appId");
                        App r = apps.stream().filter(app -> Objects.equals(app.getSourceKey(), appId)).findFirst().orElse(null);
                        if(r != null) {
                            item.put("appName", r.getName());
                            item.put("isOa", Objects.equals(r.getSourceKey(), OA_APPID));
                        }


                        rows.add(item);
                    }
                }
            }

        }


//        List<HashMap<String, Object>> rows = new ArrayList<>();
//
//        if(tasks != null && !tasks.isEmpty()) {
//            List<String> appIds = new ArrayList<>();
//            HashMap<String, List<String>> appIdToTaskIds = new HashMap<>();
//
//            for(HashMap item : tasks) {
//                if(Objects.equals((String) item.get("isExpireStatus"), "0")) {
//                    continue;
//                }
//                String appId = (String) item.get("appId");
//                if(appId != null && !appId.isEmpty()) {
//                    if(!appIds.contains(appId)) {
//                        appIds.add(appId);
//                    }
//
//                    String taskId = (String) item.get("taskId");
//
//                    if(appIdToTaskIds.containsKey(appId)) {
//                        appIdToTaskIds.get(appId).add(taskId);
//                    }else {
//                       List<String> taskIds = new ArrayList<>();
//                       taskIds.add(taskId);
//                        appIdToTaskIds.put(appId, taskIds);
//                    }
//                }
//            }
//
//            if(appIds.isEmpty()) return ResponseUtil.successRes(rows);
//
//            List<App> apps = db.app.list(new LambdaQueryWrapper<App>().select(App::getId, App::getSourceKey, App::getName, App::getCover, App::getBccolor).eq(App::isTaskApp, true).in(App::getSourceKey, appIds));
//            if(!apps.isEmpty()) {
//
//                for(App app : apps) {
//                    List<String> taskIds = appIdToTaskIds.get(app.getSourceKey());
//                    if(taskIds != null) {
//                        tasks = tasks.stream().filter(v -> taskIds.contains((String) v.get("taskId"))).collect(Collectors.toList());
//
//                        HashMap<String, Object> row = new HashMap<>();
//                        row.put("name", app.getName());
//                        row.put("cover", app.getCover());
//                        row.put("bccolor", app.getBccolor());
//                        row.put("tasks", tasks);
//
//                        rows.add(row);
//                    }
//                }
//            }
//
//        }

        return ResponseUtil.successRes(rows);
    }

    @GetMapping("detail/{id}")
    public Object task(@PathVariable("id") String id, @RequestAttribute("userKey") String userKey) {
        HashMap<String, Object> task = openapi.task(userKey, id);
        return ResponseUtil.successRes(task);
    }
}
